package com.tanhua.autoconfig.template;

import com.baidu.aip.face.AipFace;
import com.tanhua.autoconfig.properties.AipFaceProperties;
import lombok.Data;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;

public class AipFaceTemplate {

    @Autowired
    private AipFace client;
    /**
     * 检测图片中是否包含人脸
     *      true：包含
     *      false：不包含
     * @return
     */
    public boolean detect(String image){
        // 调用接口
        //String image = "https://tanhua-dcl.oss-cn-beijing.aliyuncs.com/2021/11/08/2888839c-1123-47fa-a1a1-ccdaaa0038c7.png";
        String imageType = "URL";

        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
        Integer error_code = (Integer) res.get("error_code");
        return error_code==0;
    }
}